<?php
/**
 * Created by PhpStorm.
 * User: g
 * Date: 2017/5/26
 * Time: 13:54
 */
use think\Db;


function covertstr2arr($str,$pattern){
    $result = array();
    $params = explode ($pattern,$str);
    for($i = 0;$i < count($params);$i=$i+2){
        $result[$params[$i]] = $params[$i+1];
    }
    return $result;
}
function get_top_menu($rule, $type = 0,$level = 1) {
    $menu = Db::name("Menu")->where("rule_value",$rule)->find();
    if($menu['level'] <= $level) {
        if($type == 0) {
            return $rule;
        }else {
            return $menu;
        }
    }else {
        $m2 = Db::name("Menu")->where("id", $menu['menu_pid'])->find();
        return get_top_menu($m2['rule_value'],$type, $level);
    }
}
function get_sub_menu($auth_id, $pid) {
    if($auth_id == 1){
        $authRecord = Db::name("Menu")->where('status',1)->where("option","menu")->where('type in (0,1)')->where('menu_pid',$pid)->field('id,menu_pid,name,listorder,icon,rule_value,data')->order('listorder ASC')->select();
    }else{
        $authRecord = Db::name("AuthRecord")
            ->where('a.auth_id',$auth_id)
            ->alias('a')
            ->join('menu m','(a.rule_value = m.rule_value and m.status = 1 and m.menu_pid = '.$pid.' and m.option="menu" and m.type in (0,1))')
            ->field('m.id,m.menu_pid,m.name,m.listorder,m.icon,a.rule_value,m.data')->order('m.listorder ASC')->select();
    }
    return $authRecord;
}

function get_auth_button($auth_id, $rule) {
    $authRecord = array();
    $pmenu = Db::name("Menu")->where("rule_value", $rule)->find();
    $ret = array();
    if($pmenu) {
        if ($auth_id == 1) {
            $authRecord = Db::name("Menu")->where('status', 1)->where("option", "button")->where('type in (0,1)')->where('menu_pid', $pmenu['id'])->field('rule_value')->order('listorder ASC')->select();
        } else {
            $authRecord = Db::name("AuthRecord")
                ->where('a.auth_id', $auth_id)
                ->alias('a')
                ->join('menu m', '(a.rule_value = m.rule_value and m.status = 1 and m.menu_pid = ' . $pmenu['id'] . ' and m.option="button" and m.type in (0,1))')
                ->field('a.rule_value')->order('m.listorder ASC')->select();
        }

        foreach ($authRecord as $item) {
            array_push($ret, $item['rule_value']);
        }
    }
    return $ret;
}

function  get_auth_menu($auth_id,$pid, $rule, $level = 1){
    $menu = '';
    $current_menu = strtolower(get_top_menu($rule, 0, $level));

    if($auth_id == 1){
        $authRecord = Db::name("Menu")->where('status',1)->where("option","menu")->where('type in (0,1)')->where('menu_pid',$pid)->field('id,menu_pid,name,listorder,icon,rule_value,data')->order('listorder ASC')->select();
    }else{
        $authRecord = Db::name("AuthRecord")
            ->where('a.auth_id',$auth_id)
            ->alias('a')
            ->join('menu m','(a.rule_value = m.rule_value and m.status = 1 and m.menu_pid = '.$pid.' and m.option="menu" and m.type in (0,1))')
            ->field('m.id,m.menu_pid,m.name,m.listorder,m.icon,a.rule_value,m.data')->order('m.listorder ASC')->select();
    }
    if($authRecord){
        foreach ($authRecord as $item) {
            $url = '';
            if(substr($item['rule_value'],strripos($item['rule_value'],"/") + 1)=="default") {
                $sub =  get_sub_menu($auth_id, $item['id']);
                if(count($sub) > 0) {
                    $s = $sub[0];
                    $url = url($s['rule_value'], empty($s['data']) ? [] : covertstr2arr($s['data'], "/"));
                }
            }else {
                $url = url($item['rule_value'], empty($item['data']) ? [] : covertstr2arr($item['data'], "/"));
            }
            $menu .= "<li class = 'layui-nav-item ".($item['rule_value'] == $current_menu?"layui-this":"")."'>";
            $menu .=  "<a   href = '{$url}'>" . $item['name'] . "</a>";
            $menu .= "</li>";
        }
    }
    return $menu;
}
function get_tree_view($list) {
    $html = '';
    foreach($list as $item) {
        if(count($item['child']) > 0) {
            $html .= "<div action-groups='{$item['id']}'><input lay-filter = 'group' lay-skin='primary' type='checkbox' {$item['checked']} value='{$item['id']}' title='{$item['name']}' name='menuid[]'/><div class = 'mgnl24'>";
            $html .= get_tree_view($item['child']);
            $html .= "</div></div>";
        }else {
            $html .= "<span class = 'u-box'><input action-type='child' type = 'checkbox' lay-skin='primary' action-group='{$item['id']}' {$item['checked']} value='{$item['id']}' title='{$item['name']}' name='menuid[]' /></span>";
        }
    }
    return $html;
}

